package xymt.novaway.system.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import xymt.novaway.common.idempotent.annotation.RepeatSubmit;
import xymt.novaway.common.log.annotation.Log;
import xymt.novaway.common.web.core.BaseController;
import xymt.novaway.common.mybatis.core.page.PageQuery;
import xymt.novaway.common.core.domain.R;
import xymt.novaway.common.core.validate.AddGroup;
import xymt.novaway.common.core.validate.EditGroup;
import xymt.novaway.common.log.enums.BusinessType;
import xymt.novaway.common.excel.utils.ExcelUtil;
import xymt.novaway.system.domain.vo.SysOptionVo;
import xymt.novaway.system.domain.bo.SysOptionBo;
import xymt.novaway.system.service.ISysOptionService;
import xymt.novaway.common.mybatis.core.page.TableDataInfo;

/**
 * 网站配置
 *
 * @author NovaWay
 * @date 2025-07-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/option")
public class SysOptionController extends BaseController {

    private final ISysOptionService sysOptionService;

    /**
     * 根据分类获取配置列表
     */
    @GetMapping("/category/{category}")
    public R<Map<String, String>> listByCategory(@PathVariable String category) {
        return R.ok(sysOptionService.listByCategory(category));
    }

    /**
     * 重置指定分类的配置为默认值
     */
    @SaCheckPermission("system:option:edit")
    @Log(title = "网站配置", businessType = BusinessType.UPDATE)
    @PutMapping("/reset/{category}")
    public R<Void> resetByCategory(@PathVariable String category) {
        return toAjax(sysOptionService.resetByCategory(category));
    }

    /**
     * 批量更新配置
     */
    @SaCheckPermission("system:option:edit")
    @Log(title = "网站配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateBatch")
    public R<Void> updateBatch(@RequestBody List<SysOptionBo> boList) {
        boolean success = true;
        for (SysOptionBo bo : boList) {
            success = success && sysOptionService.updateByCode(bo);
        }
        return toAjax(success);
    }

    /**
     * 查询网站配置列表
     */
    @SaCheckPermission("system:option:list")
    @GetMapping("/list")
    public TableDataInfo<SysOptionVo> list(SysOptionBo bo, PageQuery pageQuery) {
        return sysOptionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出网站配置列表
     */
    @SaCheckPermission("system:option:export")
    @Log(title = "网站配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysOptionBo bo, HttpServletResponse response) {
        List<SysOptionVo> list = sysOptionService.queryList(bo);
        ExcelUtil.exportExcel(list, "网站配置", SysOptionVo.class, response);
    }

    /**
     * 获取网站配置详细信息
     *
     * @param optionId 主键
     */
    @SaCheckPermission("system:option:query")
    @GetMapping("/{optionId}")
    public R<SysOptionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long optionId) {
        return R.ok(sysOptionService.queryById(optionId));
    }

    /**
     * 新增网站配置
     */
    @SaCheckPermission("system:option:add")
    @Log(title = "网站配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOptionBo bo) {
        return toAjax(sysOptionService.insertByBo(bo));
    }

    /**
     * 修改网站配置
     */
    @SaCheckPermission("system:option:edit")
    @Log(title = "网站配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOptionBo bo) {
        return toAjax(sysOptionService.updateByBo(bo));
    }

    /**
     * 删除网站配置
     *
     * @param optionIds 主键串
     */
    @SaCheckPermission("system:option:remove")
    @Log(title = "网站配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{optionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] optionIds) {
        return toAjax(sysOptionService.deleteWithValidByIds(List.of(optionIds), true));
    }
}
